package com.example.springboot.config.拦截器;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class RedisUrlInterceptor implements HandlerInterceptor {
//详见com.example.springboot.controller.视图解析.goIndex
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        redisTemplate.opsForValue().increment(uri);//默认每次访问uri +1
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
